package cn.edu.sdjzu.knoverse.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.edu.sdjzu.knoverse.model.entity.kg.CourseNode;
import cn.edu.sdjzu.knoverse.model.entity.kg.UserNode;
import cn.edu.sdjzu.knoverse.model.vo.result.PageResult;
import cn.edu.sdjzu.knoverse.model.vo.result.Result;
import cn.edu.sdjzu.knoverse.model.vo.result.ResultCodeEnum;
import cn.edu.sdjzu.knoverse.service.CourseService;
import cn.edu.sdjzu.knoverse.utils.context.BaseContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/courses")
@RequiredArgsConstructor
@Tag(name = "课程管理", description = "课程相关接口")
public class CourseController {

    private final CourseService courseService;

    @GetMapping
    @SaCheckPermission("course::list")
    @Operation(summary = "获取课程列表")
    public Result<PageResult<CourseNode>> getCourseList(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String code,
            @RequestParam(required = false) String description,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(courseService.getCourseList(name, code, description, currentPage, pageSize));
    }
    
    @GetMapping("/options")
    @SaCheckPermission("course::options")
    @Operation(summary = "获取所有课程选项，用于下拉选择")
    public Result<List<CourseNode>> getCourseOptions() {
        // 检查当前用户是否有权限获取所有课程
        boolean hasAllPermission = StpUtil.hasPermission("course::all");
        Long userId = BaseContext.getUserId();
        
        // 根据权限返回不同范围的课程
        if (hasAllPermission) {
            // 有权限返回所有课程
            return Result.success(courseService.getAllCourses());
        } else {
            // 没有权限只返回自己教授的课程
            return Result.success(courseService.getCoursesByTeacherId(userId));
        }
    }
    
    @PostMapping
    @SaCheckPermission("course::add")
    @Operation(summary = "创建课程")
    public Result<CourseNode> createCourse(@RequestBody CourseNode course) {
        return Result.success(courseService.createCourse(course), ResultCodeEnum.ADD_SUCCESS);
    }

    @PutMapping("/{id}")
    @SaCheckPermission("course::update")
    @Operation(summary = "更新课程")
    public Result<CourseNode> updateCourse(@PathVariable Long id, @RequestBody CourseNode course) {
        course.setNodeId(id);
        return Result.success(courseService.updateCourse(course), ResultCodeEnum.UPDATE_SUCCESS);
    }

    @DeleteMapping("/{id}")
    @SaCheckPermission("course::delete")
    @Operation(summary = "删除课程")
    public Result<Object> deleteCourse(@PathVariable Long id) {
        courseService.deleteCourse(id);
        return Result.success(null, ResultCodeEnum.DELETE_SUCCESS);
    }

    @GetMapping("/{id}")
    @SaCheckPermission("course::detail")
    @Operation(summary = "获取课程详情")
    public Result<CourseNode> getCourse(@PathVariable Long id) {
        return Result.success(courseService.getCourse(id));
    }

    @PutMapping("/{courseId}/permissions")
    @SaCheckPermission("course::update-permissions")
    @Operation(summary = "更新课程的授权用户列表")
    public Result<Object> updateCoursePermissions(
            @PathVariable Long courseId,
            @RequestBody List<Long> userIds) {
        courseService.updateCourseAccess(courseId, userIds);
        return Result.success(null, ResultCodeEnum.UPDATE_SUCCESS);
    }

    @GetMapping("/{courseId}/permissions")
    @SaCheckPermission("course::access")
    @Operation(summary = "获取课程访问权限列表")
    public Result<Set<UserNode>> getCoursePermissions(@PathVariable Long courseId) {
        return Result.success(courseService.getCourseAccessList(courseId));
    }
    
    @PutMapping("/{courseId}/prerequisites")
    @SaCheckPermission("course::update-prerequisites")
    @Operation(summary = "更新课程的前置课程列表")
    public Result<Object> updateCoursePrerequisites(
            @PathVariable Long courseId,
            @RequestBody List<Long> preCourseIds) {
        courseService.updatePreCourses(courseId, preCourseIds);
        return Result.success(null, ResultCodeEnum.UPDATE_SUCCESS);
    }
    
    @GetMapping("/{courseId}/prerequisites")
    @SaCheckPermission("course::prerequisites")
    @Operation(summary = "获取课程的前置课程列表")
    public Result<Set<CourseNode>> getCoursePrerequisites(@PathVariable Long courseId) {
        return Result.success(courseService.getPreCourses(courseId));
    }
} 